package com.zsck.pos.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.Observer;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.chinaums.commoncheck.R;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.zsck.pos.adapter.BillFeeAdapter;
import com.zsck.pos.adapter.SelectedCallBack;
import com.zsck.pos.base.BaseTitleActivity;
import com.zsck.pos.bean.Bill;
import com.zsck.pos.bean.FeeEvent;
import com.zsck.pos.bean.FeeListBean;
import com.zsck.pos.bean.Record;
import com.zsck.pos.net.MyObserver;
import com.zsck.pos.net.RetrofitCilent;
import com.zsck.pos.utils.LogUtil;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.zsck.net.Utils.RxHelper.observableIO2Main;

public class BillActivity extends BaseTitleActivity implements SelectedCallBack {

    /*房间号*/
    @BindView(R.id.tv_room)
    TextView mTvRoom;
    /*本月待缴金额*/
    @BindView(R.id.tv_money)
    TextView mTvMoney;
    /*时间*/
    @BindView(R.id.tv_time)
    TextView mTvTime;
    /*屋主*/
    @BindView(R.id.tv_name)
    TextView mTvName;
    /*已缴金额(元)*/
    @BindView(R.id.tv_amount_paid)
    TextView mTvAmountPaid;
    /*账单金额(元)*/
    @BindView(R.id.tv_amount_bill)
    TextView mTvAmountBill;
    /*费用列表*/
    @BindView(R.id.lv_fee_list)
    ListView mLvFeeList;
    /*全选*/
    @BindView(R.id.cb_all_select)
    CheckBox mCbAllSelect;
    /*合计*/
    @BindView(R.id.tv_total_fee)
    TextView mTvTotalFee;
    /*去收款*/
    @BindView(R.id.btn_next)
    Button mBtnNext;

    private String mBillIdList;

    private BillFeeAdapter adapter;

    /*合计金额*/
    private double total = 0.0d;

    private List mSelecteFee;
    private Bill mBill;
    private String projectId;

    @Override
    public int setLayout() {
        return R.layout.activity_bill;
    }

    @Override
    public String setTitle() {
        return "";
    }

    @Override
    public Activity setActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initEvents();
        initData();
    }


    private void initEvents() {
        mCbAllSelect.setOnClickListener(this);
        mBtnNext.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = getIntent();
        mBillIdList = intent.getStringExtra("idList");
        projectId = intent.getStringExtra("projectId");
        getDetail();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_next:
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName("com.ums.sz.pos", "com.zsck.pos.activity.PayActivity");
                intent.putExtra("amount", total);
                intent.putExtra("projectId", projectId);
                intent.putExtra("payer", mBill.getCheckInPer());
                intent.putExtra("billIdList", (Serializable) mBill.getBillIdList());
                intent.putExtra("feeList", (Serializable) mSelecteFee);
                intent.setComponent(componentName);
                startActivity(intent);
                break;
            case R.id.cb_all_select:
                if (adapter != null) {
                    adapter.allSelectedSwitch();
                }
                break;
        }
    }


    private void getDetail() {
        List<String> list = new Gson().fromJson(mBillIdList, new TypeToken<ArrayList<String>>() {
        }.getType());
        MyObserver<Bill> observer = new MyObserver<Bill>(this) {
            @Override
            public void onSuccess(Bill result) {
                onGetinDetail(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                LogUtil.e("test", e.getMessage());
                Toast.makeText(BillActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void reRequest() {
                getDetail();
            }
        };
        RetrofitCilent.getApiService().getDetail(list).
                compose(observableIO2Main(this)).subscribe(observer);
    }


    private void onGetinDetail(Bill result) {
        if (result == null) {
            return;
        }
        mBill = result;
        changeTitle(result.getMonth() + "账单");
        mTvRoom.setText(result.getProjectName() + "-" + result.getRoomName());
        mTvMoney.setText(result.getGetTotal() + "");
        mTvTime.setText("缴纳期限：" + result.getPayDateDescription());
        mTvName.setText(result.getCheckInPer());
        mTvAmountPaid.setText(result.getReceiveAmount() + "");
        mTvAmountBill.setText(result.getBillAmount() + "");
        adapter = new BillFeeAdapter(this, result.getFeeList());
        mLvFeeList.setAdapter(adapter);
    }


    @Override
    public void onSelectedChange(List selectedIndex, boolean isAllSelected) {
        mCbAllSelect.setChecked(isAllSelected);
        mSelecteFee = selectedIndex;
        total = 0.0d;
        for (Object object : selectedIndex) {
            if (!(object instanceof FeeListBean)) {
                continue;
            }
            FeeListBean item = (FeeListBean) object;
            BigDecimal b1 = new BigDecimal(Double.toString(item.getAmount()));
            BigDecimal b2 = new BigDecimal(Double.toString(total));
            total = b1.add(b2).doubleValue();
//            total += item.getAmount();

        }
        LogUtil.e("TOTAL", "" + total);
        mTvTotalFee.setText("¥" + total);
        mBtnNext.setEnabled(total > 0);
    }


}
